<?php

/**
 * tis-tis 淘宝api相关函数库
 *
 * @copyright (c)2010 昆明力行软件  保留所有权利。
 * @link http://www.tis-tis.com
 * @Author: qiyan
 * @version: 1.0.0
 */

include_once dirname(__FILE__).'/libs/api/Taoapi.php';

$Taoapi_Config = Taoapi_Config::Init();
$Taoapi_Config->setCharset('UTF-8');


function get_category_tree($nick){
	$Taoapi = new Taoapi;

	$Taoapi->method = 'taobao.sellercats.list.get';

	$Taoapi->nick = $nick;

	$data = $Taoapi->Send('get','json')->getArrayData();
	$catArray = $data[sellercats_list_get_response][seller_cats][seller_cat];
	foreach ($catArray as $row){

		if ($row['parent_cid']=== '0' && strpos($row['name'], '统计')===false && strpos($row['name'], '分析')===false){
			$hasChild = false;	
			$row['url'] = 'category.php?cid='.$row['cid'];
			foreach ($catArray as $cat){
				if ($cat['parent_cid'] === $row['cid']){
					$row['url'] .= ','.$cat['cid'];
					$hasChild = true;
				}
			}
			if ($hasChild){
				$row['url'] = str_replace($row['cid'].',','',$row['url']) ;
			}			
			$res[$row['cid']] = $row;
			
			foreach ($catArray as $cat){
				$cat['url'] = 'category.php?cid='.$cat['cid'];
				if ($cat['parent_cid'] === $row['cid']){
					$res[$row['cid']]['child'][$cat['cid']]= $cat;
				}
			}
		}


	}

	return $res;

}

function get_goodslist_bycid($cid=0,$page=1){
	$Taoapi = new Taoapi;
	$Taoapi->method = 'taobao.items.all.get';
	$Taoapi->fields = 'iid,title,nick,pic_url,cid,price,type,delist_time,post_fee';

	$Taoapi->seller_cids = $cid;
	//$Taoapi->page_no
	//$Taoapi->order_by = 'volume';

	$data = $Taoapi->Send('get','json')->getArrayData();
	$itemArray = $data[items_all_get_response][items][item];
	foreach ($itemArray as $item){
		$item['url'] = 'goods.php?iid='.$item['iid'];
		$res[$item['iid']]	= $item;
	}
	return $res;
}

function get_goods_info($nick,$iid){
	$Taoapi = new Taoapi;
	$Taoapi->method = 'taobao.item.get';
	$Taoapi->fields = 'iid,detail_url,num_iid,title,nick,type,pic_url,
		desc,num,valid_thru,list_time,delist_time,location,price,post_fee,express_fee,ems_fee,item_img';

	$Taoapi->iid = $iid;
	$Taoapi->nick = $nick;

	$data = $Taoapi->Send('get','xml')->getArrayData();
	$res = $data[item];

	return $res;

}

function get_goodslist_bykey($nick,$key,$page=1){
	$Taoapi = new Taoapi;
	$Taoapi->method = 'taobao.items.get';
	$Taoapi->fields = 'iid,title,nick,pic_url,cid,price,type,delist_time,post_fee';

	$Taoapi->nicks = $nick;
	$Taoapi->q = $key;

	$data = $Taoapi->Send('get','json')->getArrayData();
	$itemArray = $data[items_get_response][items][item];
	foreach ($itemArray as $item){
		$item['url'] = 'goods.php?iid='.$item['iid'];
		$res[$item['iid']]	= $item;
	}
	return $res;
}

function get_new_goods($nick){
	$Taoapi = new Taoapi;
	$Taoapi->method = 'taobao.items.get';
	$Taoapi->fields = 'iid,title,nick,pic_url,cid,price,type,delist_time,post_fee';

	$Taoapi->nicks = $nick;
	$Taoapi->page_size = 5;

	$data = $Taoapi->Send('get','json')->getArrayData();
	$itemArray = $data[items_get_response][items][item];
	foreach ($itemArray as $item){
		$item['url'] = 'goods.php?iid='.$item['iid'];
		$res[$item['iid']]	= $item;
	}
	return $res;
}

function get_best_goods($nick){
	$Taoapi = new Taoapi;
	$Taoapi->method = 'taobao.items.get';
	$Taoapi->fields = 'iid,title,nick,pic_url,cid,price,type,delist_time,post_fee';

	$Taoapi->nicks = $nick;
	$Taoapi->page_size = 12;
	$Taoapi->order_by = 'volume:desc';


	$data = $Taoapi->Send('get','json')->getArrayData();
	$itemArray = $data[items_get_response][items][item];
	foreach ($itemArray as $item){
		$item['url'] = 'goods.php?iid='.$item['iid'];
		$res[$item['iid']]	= $item;
	}
	return $res;
}

function get_shop_info($nick){
	$Taoapi = new Taoapi;
	$Taoapi->method = 'taobao.shop.get';
	$Taoapi->fields = 'sid,cid,title,nick,desc,bulletin,pic_path,created,modified';

	$Taoapi->nick = $nick;

	$data = $Taoapi->Send('get','json')->getArrayData();
	$res = $data[shop_get_response][shop];

	return $res;
}

?>